<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use Illuminate\Http\Request;

class BannerController extends Controller
{
    //

    public function index(Request $request)
    {
        $banners = Banner::query()->where('state',1)->orderByDesc('sort')->get();

        return $this->response($banners);
    }

    public function store(Request $request)
    {
        $data = $request->all();

        $data['state'] = 1;

        $banner = Banner::query()->create($data);

        return $this->response($banner,'添加成功');
    }

    public function show($id)
    {
        return $this->response(Banner::query()->find($id));
    }

    public function update($id, Request $request)
    {
        $data = $request->all();

        $banner = Banner::query()->where('id', $id)->first();

        if (!$banner) return $this->response([],'轮播图未找到',500);

        $res = $banner->update($data);

        return $res ? $this->response($res,'修改成功') : $this->response($res,'修改失败');
    }

    public function destroy($id)
    {
        $banner = Banner::query()->where('id', $id)->first();

        if (!$banner) return $this->response([],'轮播图未找到',500);

        $res = $banner->delete();

        return $res ? $this->response($res,'删除成功') : $this->response($res,'删除失败');
    }
}
